package com.example.controller;

import com.example.common.AppResult;
import com.example.common.ResultCode;
import com.example.config.AppConfig;
import com.example.model.Article;
import com.example.model.ArticleReply;
import com.example.model.User;
import com.example.service.IArticleReplyService;
import com.example.service.IArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:帖子回复接口
 * User: bjc
 * Date: 2025-10-16
 * Time: 21:09
 */
@Tag(name = "回复接口")
@Slf4j
@RestController
@RequestMapping("/articleReply")
public class ArticleReplyController {
    @Autowired
    private IArticleReplyService articleReplyService;
    @Autowired
    private IArticleService articleService;

    @Operation(summary = "创建帖子回复")
    @PostMapping("/create")
    public AppResult createArticleReply(HttpServletRequest  request,
                                        @Parameter(description = "帖子Id") @RequestParam("articleId") @NonNull Long articleId,
                                        @Parameter(description = "回复内容") @RequestParam("content") @NonNull String content) {
        //1.获取用户信息
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //2.判断用户状态
        if(user.getState() == 1){
            //表示用户被禁言，不能发表回复
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //3.获取帖子信息
        Article article = articleService.selectById(articleId);
        if(article == null || article.getDeleteState() == 1){
            //帖子不存在
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());

            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(article.getState() == 1){
            //帖子被禁言，不能发表回复
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //4.创建帖子回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId); //要回复的帖子id
        articleReply.setPostUserId(user.getId()); //回复的用户id
        articleReply.setContent(content);
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        //5.调用业务层
        articleReplyService.createArticleReply(articleReply);
        //6.返回结果
        return AppResult.success();
    }
    @Operation(summary = "根据帖子id查询帖子回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(
            @Parameter(description = "帖子id") @RequestParam("articleId") @NonNull Long articleId){
        //1.查询帖子是否存在
        Article article = articleService.selectById(articleId);
        if(article == null || article.getDeleteState() == 1){
            //帖子不存在
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //2.调用业务层
        List<ArticleReply> articleReplyList = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplyList);
    }
}
